#!/bin/sh
# Copyright 2000,2016 BitMover, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# bitkeeper	Start/stop the bitkeeper daemon.
# 		%W% Copyright (c) 1997 Larry McVoy

# Source networking configuration.
if [ -f /etc/sysconfig/network ]
then	. /etc/sysconfig/network

	# Check that networking is up.
	[ ${NETWORKING} = "no" ] && exit 0
fi
[ -x /usr/bin/bk ] || exit 0
VAR=/var/bitkeeper

case "$1" in
    start_msg)	echo "Start BitKeeper daemons"
		;;
    stop_msg)	echo "Stop BitKeeper daemons"
		;;
    start)	cd $VAR || exit 1
		test -f repositories || {
			echo Nothing advertised
			exit 0
		}
		while read dir opts
		do	(
			cd $dir || exit 1
			F=`basename $dir`
			bk bkd $opts -l$VAR/log.$F -P$VAR/pid.$F
			echo Started bkd $opts in $dir
			)
		done < repositories
	    	;;

    stop)	
		cd $VAR || exit 1
		echo Shutting down BitKeeper daemons
		for i in pid.*
		do	kill -HUP `cat $i`
			rm $i
		done
		;;

    status)	ps -axf | grep bkd
		;;

    *)		echo "Usage: bitkeeper {start|stop}"
    		exit 1
		;;
esac

exit 0
